package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.view.View;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.adapter.RepairAdapter;
import com.bwie.parkdemo.mvvm.model.RepairModel;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.entity.RepairEntity;
import com.bwie.parkdemo.mvvm.model.entity.RepairUserEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 维修列表viewmodel
 * 1.适配器类型
 * 2.recyclerview中要显示的数据源
 * 3.recyclerview Item点击事件，点击查看维修详情
 * */
public class RepairViewModel extends BaseOBViewModel<RepairModel> {

    public int adapterType = 6;
    public int userAdapterType = 7;
    public ObservableField<List<Object>>datas;//databinding需要使用数据源
    public ObservableField<List<Object>>userDatas;
    private List<Integer>repairDatas;
    public RepairItemClickListener itemListener;
    public RepairChildItemClickListener childItemClickListener;
    private LoginEntity.Values user;//本地登陆的用户信息数据
    private long repairUserId = 0;//记录选中维修人员id
    private boolean flag = false;

    public boolean isFlag() {
        return flag;
    }

    public RepairViewModel(@NonNull Application application) {
        super(application);
        datas = new ObservableField<>();
        userDatas = new ObservableField<>();
        itemListener = new RepairItemClickListener();
        childItemClickListener = new RepairChildItemClickListener();
    }

    @Override
    protected RepairModel createModel() {
        return new RepairModel();
    }

    /**
     *
     * */
    @Override
    protected void initData() {
        user = m.requestUser(getApplication());
        //获取全部维修列表
        m.requestRepair().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
        //获取指定维修员工列表
        m.requestRepairUser().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity instanceof RepairEntity){
            RepairEntity entity = (RepairEntity) baseEntity;
            datas.set(entity.getDatas());
        }else if (baseEntity instanceof RepairUserEntity){
            //维修人员数据
            RepairUserEntity entity = (RepairUserEntity) baseEntity;
            userDatas.set(entity.getDatas());
        }

    }

    //指定维修人员请求
    public void appointRepair(){
        //遍历全部维修任务列表->选出对应维修任务
        repairDatas = new ArrayList<>();
        for (int i = 0;i < datas.get().size();i ++){
            RepairEntity.Values values = (RepairEntity.Values) datas.get().get(i);
            if (values.isSelect)
                repairDatas.add(values.repairId);
        }
        RequestBody[]bodyes=new RequestBody[repairDatas.size()];
        //由于服务器接口一个维修任务id+一个维修人员id为一次请求参数-》一次网络请求
        //遍历全部维修任务生成多个维修请求
        int i = 0;
        for (Integer repairId : repairDatas){
            Map<String,Object>map = new HashMap<>();
            map.put("repairId",repairId);
            map.put("repairUserId",repairUserId);
            String jsonStr = new Gson().toJson(map);
            bodyes[i] = RequestBody.create(MediaType.parse("application/json"),jsonStr);
            i ++;
        }
        m.request(bodyes).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                        showDialog();
                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        showMsg("请求出错"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        disDialog();
                        //移除上传绑定的维修任务
                        List<Object>list = new ArrayList<>();
                        for (int i = 0;i < datas.get().size();i ++){
                            RepairEntity.Values repairEntity = (RepairEntity.Values) datas.get().get(i);
                            if (repairEntity.isSelect)
                                continue;
                            list.add(repairEntity);
                        }
                        datas.set(list);
                    }
                });
    }

    //RecyclerView中Item点击
    public class RepairItemClickListener implements BaseQuickAdapter.OnItemClickListener {

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            if (adapter instanceof RepairAdapter){
                //维修列表的点击
            }else {
                //维修人员的选择
                //将列表中全部数据改编为false
                List<Object>list = new ArrayList<>();
                list.addAll(userDatas.get());
                for (int i = 0;i < list.size();i ++){
                    RepairUserEntity.Values values = (RepairUserEntity.Values) list.get(i);
                    values.isSel = false;
                    if (i == position){
                        values.isSel = true;
                        repairUserId = values.uId;
                    }
                    list.set(i,values);
                }
                userDatas.set(list);
            }
        }
    }

    //recyclerView中item中子控件监听
    public class RepairChildItemClickListener implements BaseQuickAdapter.OnItemChildClickListener{

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            List<Object>list = new ArrayList<>();
            list.addAll(datas.get());
            RepairEntity.Values values = (RepairEntity.Values) list.get(position);
            values.isSelect = !values.isSelect;
            list.set(position,values);
            datas.set(list);
            flag = true;
            for (Object object:list){
                RepairEntity.Values entity = (RepairEntity.Values) object;
                if (!entity.isSelect)
                    flag = false;
            }
            EventBus.getDefault().post(""+System.currentTimeMillis());
        }
    }

    //显示选择框
    public void showSel(){
        //改变datas下的isShowboolean值
        List<Object>list = new ArrayList<>();
        for (Object object: datas.get()){
            RepairEntity.Values values = (RepairEntity.Values) object;
            values.isShow = true;
            values.isSelect = false;
            list.add(values);
        }
        datas.set(list);
    }

    //全部选中
    public boolean selAll(){
        flag = true;
        for (Object object: datas.get()){
            RepairEntity.Values values = (RepairEntity.Values) object;
            if (!values.isSelect)
                flag = false;
        }
        List<Object>list = new ArrayList<>();
        if (flag){
            for (Object object: datas.get()){
                RepairEntity.Values values = (RepairEntity.Values) object;
                values.isShow = true;
                values.isSelect = false;
                list.add(values);
            }
            flag = false;
        }else {
            for (Object object: datas.get()){
                RepairEntity.Values values = (RepairEntity.Values) object;
                values.isShow = true;
                values.isSelect = true;
                list.add(values);
            }
            flag = true;
        }
        datas.set(list);
        return flag;
    }

}
